package pers.vic.system.system.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import pers.vic.base.attachment.service.AttachmentService;
import pers.vic.base.bo.system.SysUser;
import pers.vic.base.pager.Lookup;
import pers.vic.base.pager.PageInfo;
import pers.vic.base.service.BaseService;
import pers.vic.system.system.mapper.SysUserMapper;

/**
 * 系统用户表 Service
 * 
 * @author VIC
 */
@Service
public class SysUserService extends BaseService {

	@Resource
	private SysUserMapper sysUserMapper;

	@Resource
	private AttachmentService attachmentService;

	/**
	 * 查询列表
	 */
	public PageInfo<SysUser> list(Lookup lookup) {
		startPage(lookup);
		List<SysUser> datas = sysUserMapper.list(lookup);
		return PageInfo.instance(datas, lookup);
	}

	/**
	 * 根据主键查询
	 */
	public SysUser findById(int id) {
		return sysUserMapper.findById(id);
	}
	/**
	 * 根据用户名查询用户
	 */
	public SysUser findUserByUsername(String username) {
		return sysUserMapper.findUserByUsername(username);
	}

	/**
	 * 根据主键数组查询
	 */
	public List<SysUser> findByIds(int... ids) {
		return sysUserMapper.findByIds(ids);
	}

	/**
	 * 新增
	 */
	@Transactional
	public int insert(SysUser entity) {
		attachmentService.addAttachmengFromObj(entity);
		return sysUserMapper.insert(entity);
	}

	/**
	 * 修改
	 */
	@Transactional
	public int update(SysUser entity) {
		SysUser old = findById(entity.getId());
		attachmentService.handleOldAndNowAttachment(old, entity);
		return sysUserMapper.update(entity);
	}

	/***
	 * 删除
	 */
	@Transactional
	public int delete(int... ids) {
		List<SysUser> olds = findByIds(ids);
		attachmentService.deleteAttachmengFromObj(olds);
		return sysUserMapper.delete(ids);
	}

	/**
	 * 根据用户名查找
	 * @param id
	 * @param name
	 * @return
	 */
	public boolean checkName(Integer id, String username) {
		return sysUserMapper.checkName(id, username);
	}

	


}
